﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Paxman.Misc;
using Paxman.Objects;

namespace Paxman.Screens
{
    class Level
    {        
        Map map;
        Player player;
        List<Enemy> listEnemies;

        Texture2D txLogo;

        /// <summary>Level atual</summary>
        int currentLevel;

        const int TOTAL_MAPS = 1;

        public Level()
        {
            map = new Map();
            map.SetOffset(Util.borderLeftGameArea, Util.borderTopGameArea);
            player = new Player(map);
            currentLevel = 1;
        }

        /// <summary>Inicia o primeiro level</summary>
        public void StartFirstLevel()
        {
            currentLevel = 1;
            map.LoadMap(currentLevel);
            player.SetInitialPosition();
            foreach (Enemy enemy in listEnemies)
                enemy.SetInitialPosition();
        }

        /// <summary>Inicia o próximo level</summary>
        public void StartNextLevel()
        {
            if (currentLevel < TOTAL_MAPS)
            {
                currentLevel++;
                map.LoadMap(currentLevel);
                player.SetInitialPosition();
                foreach (Enemy enemy in listEnemies)
                    enemy.SetInitialPosition();
            }
        }

        /// <summary>Verifica se é o ultimo level</summary>
        public bool CheckLastLevel()
        {
            if (currentLevel == TOTAL_MAPS)
                return true;
            else
                return false;
        }

        public void LoadContent(ContentManager content)
        {
            txLogo = content.Load<Texture2D>("logo");
            map.LoadContent(content);
            player.LoadTexture(content, "player");
            Enemy.LoadContent(content, map);

            // é preciso criar aqui para ter acesso a textura
            listEnemies = new List<Enemy>();
            for (int i = 0; i < 4; i++)
                listEnemies.Add(new Enemy(i + 1));
        }

        public void Update(GameTime gameTime)
        {
            player.Update();
            //foreach (Enemy enemy in listEnemies)
            //    enemy.Update(gameTime);
            listEnemies[0].Update(gameTime, player.Center);
        }

        public void Draw(GameTime gameTime, SpriteBatch spritebatch)
        {
            map.Draw(spritebatch);
            player.Draw(gameTime, spritebatch);
            foreach (Enemy enemy in listEnemies)
                enemy.Draw(gameTime, spritebatch);
            spritebatch.Draw(txLogo, new Vector2(Util.borderLeftGameArea + map.Width + Util.borderLeftGameArea, Util.borderTopGameArea), Color.White);
        }
    }
}
